﻿/**
* @projectName   cvvision
* @file          IAlgorithmPluginOperator
* @brief         算法插件操作器
* @author        GuoFeng
* @contact        805305774@qq.com/feng.guo@micro-i.com.cn
* @date          2022-01-06
*/

#ifndef __I_ALGORITHM_PLUGIN_OPERATOR_H__
#define __I_ALGORITHM_PLUGIN_OPERATOR_H__

#include <QObject>
#include <QImage>
#include <QList>
#include <QPolygon>
#include <QHash>
#include <QMutex>

#include "Interface_Global.h"
#include "IAlgorithmPluginParam.h"
#include "IPluginParam.h"


namespace CVV
{

namespace Interface
{


class INTERFACE_EXPORT IAlgorithmPluginOperator : public QObject
{
    Q_OBJECT

public:
    explicit IAlgorithmPluginOperator(QObject* parent = nullptr);
    virtual ~IAlgorithmPluginOperator();

    /**
     * @brief operatorName 获取算子名称
     * @return
     */
    QString operatorName() const;

    /**
     * @brief setOperatorName 设置算子名称
     * @param peratorName
     */
    void setOperatorName(const QString &peratorName);

    /**
     *  @solt   RunOperator
     *  @brief  运行算子
     *  @param  param 算法参数
     *  @param  pResult 算法输入输出参数
     */
    virtual bool RunOperator(std::shared_ptr<IAlgorithmPluginParam> param, std::shared_ptr<IPluginParam> pResult) = 0;
    /**
     * @brief initialize
     * 初始化算子数据到内存中
     */
    virtual void initialize(const QString& flowId, const QString &cellId) = 0;

    /**
     * @brief getAlgorithmPluginParam
     * @return
     */
    virtual std::shared_ptr<IAlgorithmPluginParam> getAlgorithmPluginParam() = 0;

    /**
     * @brief getAlgorithmPluginResultOutput
     * @return
     */
    virtual std::shared_ptr<IPluginParam> getAlgorithmPluginResultOutput();

    /**
     * @brief getAlgorithmPluginParam 获取算法参数
     * @param pluginCode
     * @return
     */
    std::shared_ptr<IAlgorithmPluginParam> getAlgorithmPluginParam(const QString &flowId, const QString& cellId);

    /**
     * @brief getAlgorithmPluginResultOutput
     * @param flowCellId
     * @param pluginCode
     * @return
     */
    std::shared_ptr<IPluginParam> getAlgorithmPluginResultOutput(const QString &flowId, const QString& cellId);

    /**
     * @brief algorithmPluginParam 获取算法参数
     * @param pluginCode
     * @return
     */
    static std::shared_ptr<IAlgorithmPluginParam> algorithmPluginParam(const QString &pluginCode,
                                                                       const QString &flowId,
                                                                       const QString& cellId);

    /**
     * @brief algorithmPluginResultOutput 算法插件结果输出
     * @param flowCellId
     * @param pluginCode
     * @return
     */
    static std::shared_ptr<IPluginParam> algorithmPluginResultOutput(const QString &pluginCode,
                                                                     const QString &flowId,
                                                                     const QString& cellId);

    /**
     * @brief removeAlgorithmParam 删除指定参数
     * @param flowId
     * @param cellId
     * @return
     */
    void removeAlgorithmParam(const QString &flowId, const QString& cellId);

    /**
     * @brief removeAlgorithmParam
     * @param flowId
     */
    void removeAlgorithmParam(const QString &flowId);

    /**
     * @brief sendSoftwareTriggerCommand 相机软触发
     */
    virtual void sendSoftwareTriggerCommand(std::shared_ptr<IAlgorithmPluginParam> pResult);

    /**
     * @brief getCameraParams 相机参数
     * @param pResult
     */
    virtual void getCameraParams(std::shared_ptr<IAlgorithmPluginParam> pResult);

    /**
     * @brief enumCameras 获取相机列表
     * @param lstCamera
     */
    virtual void enumCameras(const QString &cameraType, QStringList &lstCamera);

    /**
     * @brief setCameraTriggerOff 关闭触发
     */
    virtual void setCameraTriggerOff();

    /**
     * @brief clone 克隆对象实例
     * @return
     */
    virtual IAlgorithmPluginOperator* clone() = 0;

signals:
    /**
     * @brief sigCameraPhotoReceived 收到相机图片
     * @param image
     * @param costTime
     */
    void sigCameraPhotoReceived(const QImage &image, int costTime);

protected:
    QString m_operatorName;         // 算子名称
    QHash<QString, QHash<QString, std::shared_ptr<IAlgorithmPluginParam>>> m_hashAlgorithmPluginParam;   // 算法插件参数 <flow id, <cellid, 插件参数指针>>
    QHash<QString, QHash<QString, std::shared_ptr<IPluginParam>>> m_hashPluginResultOutput;        // 算法插件结果 <flow id, <cellid, 插件结果指针>>
    QMutex m_mutex;             // 算法锁
};

}
}

#endif // __I_ALGORITHM_PLUGIN_OPERATOR_H__
